﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body id="acModel">
    <div id="msg"></div>
    <button data-bind="click:acGet">提取</button>
    <button data-bind="click:acSave">提交</button>

    <script src="Scripts/jquery-1.11.3.min.js"></script>
    <script src="Scripts/json2.js"></script>
    <script src="Scripts/base64.js"></script>
    <script src="Scripts/pidcrypt.js"></script>
    <script src="Scripts/pidcrypt_util.js"></script>
    <script src="Scripts/asn1.js"></script>
    <script src="Scripts/jsbn.js"></script>
    <script src="Scripts/rng.js"></script>
    <script src="Scripts/prng4.js"></script>
    <script src="Scripts/rsa.js"></script>
    <script src="Scripts/knockout-3.3.0.js"></script>
    <script src="Scripts/knockout.validation.js"></script>
    <script type="text/javascript">
        var uri = "http://localhost:8089/api/books";
        function acvm() {
            var self = this;
            self.Title = ko.observable().extend({ required: true, minLength: 3, maxLength: 64 });
            self.ReleaseDate = ko.observable().extend({ required: true });
            self.Genre = ko.observable().extend({ required: true, minLength: 3, maxLength: 64 });
            self.Price = ko.observable().extend({ required: true });

            var time = new Date();
            self.ReleaseDate(time);
            self.Title("titleweb");
            self.Genre("genreweb");
            self.Price(1000);

            self.getjson = function () {
                var book = new Object();
                book.Title = self.Title();
                book.Genre = self.Genre();
                book.Price = self.Price();
                book.ReleaseDate = self.ReleaseDate();
                return JSON.stringify(book);
            };

            var n_str = "o97rp70L3OLfpU0y4Zxzv2+2weogMAS0HeQgwZ5aid3Y974ARymzLpaKuFrBOJXJ6+gTIQW1rNv/TVzK7aKaVITKEX+VYYt57JS1IojlCaNTn5/i1cMYlllK8avwyI/6b1mbonCDv2LkBhs5MzyUTmdLJW8l+uZ3ecO/Ep/BoZs=";
            var e_str = "AQAB";
            var n = pidCryptUtil.convertToHex(pidCryptUtil.decodeBase64(n_str));
            var e = pidCryptUtil.convertToHex(pidCryptUtil.decodeBase64(e_str));
            var rsa = new pidCrypt.RSA();
            rsa.setPublic(n, e, 16);
            crypted = rsa.encrypt("jac:jac");
            var result = pidCryptUtil.encodeBase64(pidCryptUtil.convertFromHex(crypted));

            self.acGet = function () {
                var xhr = new XMLHttpRequest();
                xhr.open('GET', encodeURI(uri + "?_=" + Math.random()));//增加随时数到连接尾部防止浏览器缓存
                //xhr.setRequestHeader("U-ApiKey", "111");
                xhr.setRequestHeader("Authorization", "Basic " + result);
                xhr.onreadystatechange = function () {
                    if (xhr.readyState != 4) {
                        return false;
                    }
                    if (xhr.status != 200) {
                        $("#msg").html('Request failed.  Returned status of ' + xhr.status);
                        return false;
                    }
                    $("#msg").html(JSON.stringify(xhr.responseText));
                    return true;
                };
                xhr.send();
            }

            //提交(Book book)形式
            self.acSave = function () {
                if (self.isValid()) {
                    var xhr = new XMLHttpRequest();
                    xhr.open('POST', encodeURI(uri));
                    xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
                    xhr.setRequestHeader("U-ApiKey", "111");
                    xhr.setRequestHeader("Authorization", "Basic " + base64.encode("jac:jac"));
                    xhr.onreadystatechange = function () {
                        if (xhr.readyState != 4) {
                            return false;
                        }
                        if (xhr.status != 200) {
                            $("#msg").html('Request failed.  Returned status of ' + xhr.status);
                            return false;
                        }
                        var data = JSON.parse(xhr.responseText);
                        $("#msg").html('post result is ' + JSON.stringify(data));
                        return true;
                    };
                    xhr.send(self.getjson());
                } else {
                    $("#msg").html("数据验证失败！");
                    //$('#my-prompt').modal('open');
                }
            };

        }
        var dvvm = new acvm();
        ko.applyBindings(ko.validatedObservable(dvvm), document.getElementById("acModel"));
    </script>
</body>
</html>
